package com.zte.springboot_smbms.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zte.springboot_smbms.pojo.SmbmsProvider;
import com.zte.springboot_smbms.service.ProvidersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/providerController")
public class ProviderController {
    @Autowired
    private ProvidersService providersService;

    @RequestMapping("/findAllProvider")
    public String findAllProvider(Model model, String method){
        model.addAttribute("providerList",providersService.findAllProvider());
        return "providerlist";
    }

    @RequestMapping("/html_provideradd")
    public String html_provideradd(){
        return "provideradd";
    }

    @RequestMapping("getproviderlist")
    public @ResponseBody
    String getProviderlist(String method){
        if ("getproviderlist".equals(method)) {
            ObjectMapper objectMapper = new ObjectMapper();
            String resultString = null;
            try {
                resultString = objectMapper.writeValueAsString(providersService.findAllProvider());
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            return resultString;
        }
        return null;
    }

    @RequestMapping("/findProviderByParam")
    public String findProviderByParam(Model model,String queryProCode,String queryProName){
        model.addAttribute("providerList",providersService.findProviderByParam(queryProCode,queryProName));
        return "providerlist";
    }

    @RequestMapping("/findProviderById")
    public String findProviderById(Model model,Long id,String method){
        if ("providermodify".equals(method)) {
            model.addAttribute("provider",providersService.findProviderById(id).get(0));
            return "providermodify";
        }
        if ("providerview".equals(method)) {
            model.addAttribute("provider",providersService.findProviderById(id).get(0));
            return "providerview";
        }
        return "";
    }

    @RequestMapping("/delProviderById")
    public String delProviderById(Long id){
        boolean b = providersService.delProvider(id);
        if (b) {
            return "forward:findAllProvider";
        } else {
            return "providerlist";
        }

    }

    @RequestMapping("/updateProvider")
    public String updateProvider(SmbmsProvider provider) {
        boolean b = providersService.updateProvider(provider.getId(), provider);
        if (b) {
            return "forward:findAllProvider";
        } else {
            return "providerlist";
        }
    }

    @RequestMapping("/addProvider")
    public String addProvider(SmbmsProvider provider){
        boolean b = providersService.addProvider(provider);
        if (b) {
            return "forward:findAllProvider";
        } else {
            return "providerlist";
        }
    }
}
